// SINGULARITY HTML5 GAME ENGINE
// -----------------------------
// Copyright(c) 2013 by Codenova.pl

define(['../Core'], function (Core) {
    var Button = function(objButton) {
        this.BackgroundColor = objButton.BackgroundColor;
        this.BorderColor = objButton.BorderColor;
        this.BorderWidth = objButton.BorderWidth;
        this.Id = Core.GenerateUUID();
        this.Height = objButton.Height;
        this.Hovered = false;
        this.InputType = typeof objButton.InputType === 'undefined' ? 'button' : objButton.InputType;
        this.Label = objButton.Label;
        this.LabelColor = objButton.LabelColor;
        this.LabelFont = objButton.LabelFont;
        this.LabelHoverWidth = 0;
        this.LabelSize = objButton.LabelSize;
        this.LabelWidth = 0;
        this.Width = objButton.Width;
        this.X = objButton.X;
        this.Y = objButton.Y;
    };

    Button.prototype = {
        constructor: Button,

        /**
         * Adds button to the canvas
         * @param objHovered
         * @constructor
         */
        Add: function(objHovered) {
            var textX, textY;

            if(typeof objHovered === 'undefined') {
                // If LabelWidth is not set, save it to the property
                if(this.LabelWidth == 0) {
                    this.LabelWidth = Core.MeasureText(this.Label, 'normal', this.LabelFont, this.LabelSize).Width;
                }

                textX = this.X + this.LabelWidth + (this.Width - this.LabelWidth) / 2;
                textY = this.Y + (this.Height - parseInt(this.LabelSize, 10)) / 2 - 1;
                Core.DrawRectangleWithText(this.Context, this.X, this.Y, this.Width, this.Height, this.BackgroundColor, this.BorderColor, this.BorderWidth, this.Label, textX, textY, this.LabelColor, this.LabelSize, this.LabelFont, 'right');
            } else {
                // If LabelHoverWidth is not set, save it to the property
                if(this.LabelHoverWidth == 0) {
                    this.LabelHoverWidth = Core.MeasureText(objHovered.Label, 'normal', objHovered.LabelFont, objHovered.LabelSize).Width;
                }

                textX = objHovered.X + this.LabelHoverWidth + (objHovered.Width - this.LabelHoverWidth) / 2;
                textY = objHovered.Y + (objHovered.Height - parseInt(objHovered.LabelSize, 10)) / 2 - 1;
                Core.DrawRectangleWithText(this.Context, objHovered.X, objHovered.Y, objHovered.Width, objHovered.Height, objHovered.BackgroundColor, objHovered.BorderColor, objHovered.BorderWidth, objHovered.Label, textX, textY, objHovered.LabelColor, objHovered.LabelSize, objHovered.LabelFont, 'right');
            }
        },

        Create: function(objButton) {
            "use strict";
            return new Button(objButton);
        },

        Initialize: function() {
            Core.CreateCanvasForElement(this);
        }
    };

    return Button.prototype.Create;
});